package dcutils.sock;

// Import Java JDK Classes
import java.io.IOException;
import java.net.Socket;

/**
 * A client
 * @author dca
 */
public class Client {
   private Connection connection;

   public Client() throws IOException {
      this("localhost", Server.DEFAULT_PORT);
   } // END constructor

   public Client(String host, int port) throws IOException {
      System.out.printf("Connecting to server %s:%d%n", host, port);
      this.connection = new Connection(new Socket(host, port), Server.DEFAULT_BREAK_STRINGS);
      this.connection.registerAsyncInput(new AsyncCmdLine(this.connection, Server.DEFAULT_BREAK_STRINGS));
   } // END constructor

   public void sendToServer(String text) throws IOException {
      this.connection.send(text);
   } // END sendToServer

   public int getConnectionId() {
      return this.connection.getId();
   } // END getConnectionId

   public static void main(String[] args) {
      try {
         new Client();
      } catch(Exception exc) {
         System.out.printf("Warning: %s: Could not connect to server.  Message was: %s%n", exc.getClass().getSimpleName(), exc.getMessage());
         exc.printStackTrace();
      } // END try/catch
   } // END main
} // END class Client
